home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Ham Radio 2000 #2
/
Ham Radio 2000 - Volume 2.iso
/
HAMV2
/
TCP_IP
/
HAGEMAN
/
PIC_CODE
/
ANALYZER.C
next >
Wrap
C/C++ Source or Header
|
1997-10-07
|
8KB
|
423 lines
//==================================================================
//===== =====
//===== Network Analyzer PIC Interface Program =====
//===== By: Steve Hageman 12Jan97 =====
//===== =====
//==================================================================
//===== Version: 1.1 - Initial Release, 70% ROM Used 12Jan97 =====
//===== 2.0 - Changed the return 'OK' to '*' 16Feb97 =====
//===== to speed things up - 66% ROM Used =====
//===== 2.1 - I took the easy way out and swapped pin=====
//===== B3,B4 to match the schematic, this =====
//===== does not match my prototype, but it =====
//===== does match the ARRL schematic. 4Oct97 =====
//==================================================================
//-----< Initilization code >---------------------------------------
//----- Include Files -----
#include <16c71.h>
#include <stdio.h>
//----- Compiler use statements -----
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B7, rcv=PIN_A4, invert)
//----- RS232 pins -----
#define RS232_OUT PIN_B7
#define RS232_IN PIN_A4
//----- Port defines -----
#define TRIS_A 0x85
#define TRIS_B 0x86
#define PORT_A_REG 0x05
#define PORT_B_REG 0x06
#define FSR 0x04
//----- Pin defines -----
#pragma byte port_a = 0x05
#pragma byte port_b = 0x06
//----- DDS Chip Pins (45102) -----
#pragma bit SDATA = port_b.0
#pragma bit SCLK = port_b.1
#pragma bit XFER = port_b.2
//----- DDS Attenuator ------
#pragma bit SOURCE_ATTEN_A = port_b.3
#pragma bit SOURCE_ATTEN_B = port_b.4
//----- Receiver Attenuators -----
#pragma bit REFLECTION_ATTEN = port_b.5
#pragma bit TRANSMISSION_ATTEN = port_b.6
//-----< General Defines >-----
#define DOOMSDAY 0 // Not here yet!
//----- Analog A/D Channel Mapping -----
#define REF_CHAN 0
#define THRU_CHAN 1
#define PHASE_CHAN 2
//-----< Global Variables >-----
char Buf; // RS232 receive char
//-----< Subroutines >------------------------------------------------
void set_dds_frequency(void)
{
//----- Read RS232 and set frequency -----
// Loop and shift each bit into the DDS chip
while(!DOOMSDAY)
{
// Inner loop as long as char != 'E'
Buf = getc();
// If this char is a E that means end
switch(Buf)
{
case 'E':
{
// XFER Data to DDS chip output register
XFER = 0;
XFER = 1;
// Write OK back to calling program
puts("*");
return;
}
// Write a one bit to the DDS
case '1':
{
SDATA = 1;
SCLK = 1;
SCLK = 0;
SDATA = 0;
PUTS("*");
break;
}
// Write a zero bit to the DDS
case '0':
{
SDATA = 0;
SCLK = 1;
SCLK = 0;
PUTS("*");
break;
}
// Error
default:
{
puts("ERROR");
return;
}
} // End of switch
} // End of while
}
void set_dds_attenuator()
{
// Read setting from RS232
Buf = getc();
// Set DDS Attenuator
switch(Buf) {
case '1': {
// 5 dBv Max power
SOURCE_ATTEN_A = 1;
SOURCE_ATTEN_B = 1;
break;
}
case '2': {
// 0 dBv
SOURCE_ATTEN_A = 0;
SOURCE_ATTEN_B = 0;
break;
}
case '3': {
// -20 dBv
SOURCE_ATTEN_A = 1;
SOURCE_ATTEN_B = 0;
break;
}
case '4': {
// -40 dBv
SOURCE_ATTEN_A = 0;
SOURCE_ATTEN_B = 1;
break;
}
default: {
// Error
puts("ERROR");
return;
}
}
// Send OK back
puts("*");
}
read_send_channel(int chan)
{
int value;
// Set the A/D Channel, Wait for settling delay
set_adc_channel(chan);
delay_us(10);
// Read the specified A/D channel
value = read_adc();
// Send result back on RS232 as decimal, ASCII value
putc( (value/100) + '0' );
putc( ((value/10) % 10) + '0');
putc( (value%10) + '0');
}
set_attenuator(int chan)
{
// Read setting from RS232
Buf = getc();
// Set attenuator as needed
switch(Buf)
{
case '1':
{
// NOTE: A one means close relay
// setting attenuator OFF
if(chan == REF_CHAN)
{
REFLECTION_ATTEN = 0;
}
else
{
TRANSMISSION_ATTEN = 0;
}
break;
}
case '0':
{
// Set attenuator OFF
if(chan == REF_CHAN)
{
REFLECTION_ATTEN = 1;
}
else
{
TRANSMISSION_ATTEN = 1;
}
break;
}
default: {
// Error
puts("ERROR");
return;
}
} // End of switch
// Send OK back
puts("*");
}
//-----< Main >----------
void main(void)
{
// Setup Ports
#asm
clrf PORT_B_REG // Clear port b
movlw TRIS_B // Setup port B TRIS reg
movwf FSR
// Set 0-7 bits all output
bcf 00,0 // SData
bcf 00,1 // SClock
bcf 00,2 // XFER#
bcf 00,3 // DDS Range Pin A
bcf 00,4 // DDS Range Pin B
bcf 00,5 // Ref Range Relay
bcf 00,6 // Thru Range Relay
bcf 00,7 // RS232 Output
#endasm
// Setup A/D
setup_port_a(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_8); // OK for 4 MHz Clock
// Set output source attenuator to 0 dBm
SOURCE_ATTEN_A = 1;
SOURCE_ATTEN_B = 0;
// Set receiver attenuators ON
REFLECTION_ATTEN = 0;
TRANSMISSION_ATTEN = 0;
// Set initial program start DDS State
SCLK = 0;
SDATA = 0;
XFER = 1;
// Initilize RS232 pin, send a 0.
// This sets up the RS232 properly
putc(' ');
// Main loop
while(!DOOMSDAY)
{
// Wait for RS232 char -> parse to command
Buf = getc();
switch(Buf)
{
// DDS Source Parser
case 'S': {
puts("*");
// Get next char
Buf = getc();
switch(Buf) {
case 'F':
{
puts("*");
// Set frequency command
set_dds_frequency();
break;
}
case 'A':
{
puts("*");
// Set attenuator command
set_dds_attenuator();
break;
}
default:
{
puts("ERROR");
}
}
break;
}
// Ref Chan Parser
case 'R': {
puts("*");
// Get next char
Buf = getc();
switch(Buf) {
case '?':
{
puts("*");
// Read channel command
read_send_channel(REF_CHAN);
break;
}
case 'A':
{
puts("*");
// Set attenuator command
set_attenuator(REF_CHAN);
break;
}
default:
{
puts("ERROR");
}
}
break;
}
// Thru Chan Parser
case 'T': {
puts("*");
// Get next char
Buf = getc();
switch(Buf) {
case '?':
{
puts("*");
// Read channel command
read_send_channel(THRU_CHAN);
break;
}
case 'A':
{
puts("*");
// Set attenuator command
set_attenuator(THRU_CHAN);
break;
}
default:
{
puts("ERROR");
}
}
break;
}
// Phase Chan Parser
case 'P': {
puts("*");
// Get next char
Buf = getc();
switch(Buf) {
case '?':
{
puts("*");
// Read channel command
read_send_channel(PHASE_CHAN);
break;
}
default:
{
puts("ERROR");
}
}
break;
}
// Not in command list
default: {
// Error
puts("ERROR");
}
} // End of switch
} // End of while
} //----- End of main -----